//
//  String+Attributed.swift
//  TTCOM
//
//  Created by Toj on 8/28/22.
//

import Foundation

/** NSAttributedString */
public extension String {
    
    func to_attributedString(font: UIFont, color: UIColor? = nil, paragraph style: NSMutableParagraphStyle? = nil, lineStyle: CMLineStyle = .normal) -> NSMutableAttributedString {
        
        let attrs = NSAttributedString.cm_attributes(font: font, color: color, paragraph: style, lineStyle: lineStyle)
        let attributedString = NSMutableAttributedString(string: self, attributes: attrs)
        
        return attributedString
    }
    
    func to_attributedString(font: UIFont,
                             color: UIColor? = nil,
                             paragraph style: NSMutableParagraphStyle? = nil,
                             lineStyle: CMLineStyle = .normal,
                             highlight string: String,
                             hfont: UIFont? = nil,
                             hcolor: UIColor? = nil,
                             paragraph hstyle: NSMutableParagraphStyle? = nil,
                             hlineStyle: CMLineStyle = .normal) -> (attributedString: NSMutableAttributedString, range: NSRange) {
        
        // 默认富文本
        let attributedString = to_attributedString(font: font, color: color, paragraph: style, lineStyle: lineStyle)
        
        // 高亮富文本属性
        let highFont = hfont ?? font
        let highColor = hcolor ?? color
        let highStyle = hstyle ?? style
        let highlightAttributes = NSAttributedString.cm_attributes(font: highFont, color: highColor, paragraph: highStyle, lineStyle: hlineStyle)
        
        // 高亮范围
        let range = (self as NSString).range(of: string)
        // 添加高亮富文本范围属性,
        attributedString.addAttributes(highlightAttributes, range: range)
        
        return (attributedString, range)
    }
    
    func to_attributedString(norAtbs: [NSAttributedString.Key: Any], highlight string: String, hiAtbs: [NSAttributedString.Key: Any]) -> (attributedString: NSMutableAttributedString, range: NSRange) {
        
        // 默认富文本
        let attributedString = NSMutableAttributedString(string: self, attributes: norAtbs)
        
        // 高亮范围
        let range = (self as NSString).range(of: string)
        // 添加高亮富文本范围属性
        attributedString.addAttributes(hiAtbs, range: range)
        
        return (attributedString, range)
    }
}
